package com.tqyan.model.dto.paper;

import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @description Chat大纲
 * <p> <功能详细描述> </p>
 *
 * @author 陈晨
 * @date 2023/4/10 16:55
 */
@Data
public class ChatOutlineDTO implements Serializable {
    private static final long serialVersionUID = -3259118454823089760L;

    /** 一级标题 */
    private String first;
    /** 二级标题 */
    private String second;
    /** 子标题集合 */
    private List<String[]> subList;
    /** 深度 */
    private int depth;

    public ChatOutlineDTO() {
        this.subList = new ArrayList<>();
        this.depth = 1;
    }

    /**
     * @description 获取大纲集合
     * <p> <功能详细描述> </p>
     *
     * @author 陈晨
     * @date 2023/5/15 21:41
     */
    public static List<ChatOutlineDTO> getOutlineList(String content) {
        content = content.replaceAll("\n", "\\\\n");
        // 不包含两行换行, 则非大纲, 直接返回
        if (!content.contains("\\n\\n")) {
            return Collections.emptyList();
        }
        List<String> firstBlocks = new ArrayList<>(List.of(content.split("\\\\n\\\\n")));
        // 0:一行, n:多行, 移除第一行
        if (!firstBlocks.get(0).contains("\\n")) {
            boolean haveN = true;
            for (int i = 1; i <= 2; i++) {
                haveN &= firstBlocks.get(i).contains("\\n");
            }
            if (haveN) {
                firstBlocks.remove(0);
            }
        }
        // 获取大纲集合, 单行
        List<ChatOutlineDTO> outlineList = getOutlineListByRow(firstBlocks);
        // 获取大纲集合, 大标题
        if (CollectionUtils.isEmpty(outlineList)) {
            outlineList = getOutlineListByBigTitle(firstBlocks);
        }
        // 获取大纲集合
        if (CollectionUtils.isEmpty(outlineList)) {
            outlineList = getOutlineList(firstBlocks);
        }
        return outlineList;
    }

    /**
     * @description 获取大纲集合, 单行
     * <p> <功能详细描述> </p>
     *
     * @author 陈晨
     * @date 2023/5/15 21:39
     */
    public static List<ChatOutlineDTO> getOutlineListByRow(List<String> firstBlocks) {
        List<ChatOutlineDTO> outlineList = new ArrayList<>();
        ChatOutlineDTO outline = null;
        for (String firstBlock : firstBlocks) {
            // 仅处理单行内容, 发现有多行, 则直接返回
            if (firstBlock.contains("\\n")) {
                return Collections.emptyList();
            }
            if (firstBlock.indexOf("大标题") == 0) {
                outline = new ChatOutlineDTO();
                outline.setFirst(firstBlock);
                outlineList.add(outline);
                continue;
            }
            if (null != outline) {
                outline.getSubList().add(new String[]{firstBlock});
            }
        }
        return outlineList;
    }

    /**
     * @description 获取大纲集合, 大标题
     * <p> <功能详细描述> </p>
     *
     * @author 陈晨
     * @date 2023/5/15 21:39
     */
    public static List<ChatOutlineDTO> getOutlineListByBigTitle(List<String> firstBlocks) {
        List<ChatOutlineDTO> outlineList = new ArrayList<>();
        ChatOutlineDTO outline = null;
        for (String firstBlock : firstBlocks) {
            String[] secondBlocks = firstBlock.split("\\\\n");
            if (secondBlocks.length == 1) {
                if (null == outline) {
                    outline = new ChatOutlineDTO();
                    outlineList.add(outline);
                    outline.setFirst(secondBlocks[0]);
                    outline.setDepth(outline.getDepth() + 1);
                    continue;
                }
                if (outline.getDepth() == 2) {
                    outline.setSecond(secondBlocks[0]);
                    outline.setDepth(outline.getDepth() + 1);
                    continue;
                }
            }
            if (null == outline) {
                if (outlineList.size() <= 0) {
                    return Collections.emptyList();
                }
                outline = outlineList.get(outlineList.size() - 1);
            }
            outline.getSubList().add(secondBlocks);
            outline = null;
        }
        return outlineList;
    }

    /**
     * @description 获取大纲集合
     * <p> <功能详细描述> </p>
     *
     * @author 陈晨
     * @date 2023/5/15 21:39
     */
    public static List<ChatOutlineDTO> getOutlineList(List<String> firstBlocks) {
        List<ChatOutlineDTO> outlineList = new ArrayList<>();
        ChatOutlineDTO outline = new ChatOutlineDTO();
        for (String firstBlock : firstBlocks) {
            String[] secondBlocks = firstBlock.split("\\\\n");
            outline.getSubList().add(secondBlocks);
            outlineList.add(outline);
            outline = new ChatOutlineDTO();
        }
        return outlineList;
    }

}


